package com.rutas.java;

import android.app.TabActivity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.widget.TabHost;

import com.google.ads.AdRequest;
import com.google.ads.AdView;

public class Welcome extends TabActivity {
	
	private AdView adView = null;

	private boolean isNetworkAvailable(){
		ConnectivityManager cm = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
		if (activeNetwork != null)
			return true;
		else
			return false;
}
    
	private String nombre = "";	//Sirve para hacer busqueda en Tab1
	
	public String getNombre(){
		return nombre;
	}
	public void setNombre(String nombre){
		this.nombre = nombre;
	}
	
	public void switchTab(int tab){
		getTabHost().setCurrentTab(tab);
}

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Resources res = getResources(); // Resource object to get Drawables
        TabHost tabHost = getTabHost();  // The activity TabHost
        TabHost.TabSpec spec;  // Resusable TabSpec for each tab
        Intent intent;  // Reusable Intent for each tab

        
        // Create an Intent to launch an Activity for the tab (to be reused)
	        intent = new Intent().setClass(this, Tab1.class);
	        
	
	        // Initialize a TabSpec for each tab and add it to the TabHost
	        spec = tabHost.newTabSpec("tab1").setIndicator(this.getString(R.string.map),
	                          //res.getDrawable(R.drawable.ic_tab1))
	        					res.getDrawable(android.R.drawable.ic_dialog_map))
	                      .setContent(intent);
	        tabHost.addTab(spec);
	        if (isNetworkAvailable()){
		        AdView adView = (AdView)this.findViewById(R.id.adView);
		        AdRequest adRequest = new AdRequest();
		        adRequest.addTestDevice(AdRequest.TEST_EMULATOR);
		        adRequest.addTestDevice("6D9E4E60831999D0FA9C05A6ECF78CB7");
		        //adRequest.setLocation(location)
		        adView.loadAd(adRequest);
	        }

        // Do the same for the other tabs
        intent = new Intent().setClass(this, Tab2.class);

        spec = tabHost.newTabSpec("tab2").setIndicator(this.getString(R.string.senderos),
                          res.getDrawable(R.drawable.copy_unselected))
                      .setContent(intent);
        tabHost.addTab(spec);
        
        intent = new Intent().setClass(this, Tab3.class);
        spec = tabHost.newTabSpec("tab3").setIndicator(this.getString(R.string.informacion), 
        		res.getDrawable(android.R.drawable.ic_dialog_info))
        .setContent(intent);
        tabHost.addTab(spec);
        
        tabHost.setCurrentTab(0);
    }

    @Override
    public void onDestroy() {
      if (adView != null)
        adView.destroy();
      super.onDestroy();
    }
}